//@author A0094511L
#include "botlogic.h"

BotLogic& BotLogic::getInstance() {
	static BotLogic instance;
	
	return instance;
}

BotLogic::BotLogic(): _allEntries(EntriesManager::getInstance()){
	_tokeniser = NULL;
}

BotLogic::~BotLogic(){
	if(_tokeniser !=NULL){
		delete _tokeniser;
	}
}

CommonEnum::ReturnStatus BotLogic::executeCommand(string userInput){

	CommonEnum::ReturnStatus output;
	try {
		_tokeniser = new Tokeniser(userInput);
		CommonEnum::COMMAND_TYPE command = _tokeniser->getCommand();
		
		switch(command) {
			case CommonEnum::COMMAND_ADD:{
				output = executeAdd();
				break;
			}
			case CommonEnum::COMMAND_DELETE:{
				output = executeDelete();
				break;
			}
			
			case CommonEnum::COMMAND_SET_DONE:{
				output = executeSetDone();
				break;
			}
			case CommonEnum::COMMAND_EDIT:{
				output = executeEdit();
				break;
			}
			case CommonEnum::COMMAND_DISPLAY:{
				output = executeDisplay();
				break;
			}
			case CommonEnum::COMMAND_DISPLAY_ALL:{
				output = executeDisplayAll();
				break;
			}
			case CommonEnum::COMMAND_SEARCH:{
				output = executeSearch();
				break;
			}
			case CommonEnum::COMMAND_UNDO:{
				output = executeUndo();
				break;
			}
			case CommonEnum::COMMAND_REDO:{
				output = executeRedo();
				break;
			}
			case CommonEnum::COMMAND_DISPLAY_TASK:{	
				output = executeDisplayTask();
				break;
			}
			case CommonEnum::COMMAND_DISPLAY_EVENT:{
				output = executeDisplayEvent();
				break;
			}
			case CommonEnum::COMMAND_DISPLAY_WEEK: {
				output = executeDisplayWeek();
				break;
			}
			case CommonEnum::COMMAND_DISPLAY_DAY: {
				output = executeDisplayDay();
				break;
			}
			case CommonEnum::COMMAND_DISPLAY_DAY_BEFORE: {
				output = executeDisplayDayBefore();
				break;
			}
			case CommonEnum::COMMAND_CLEAR: {
				output = executeClear();
				break;
			}		
			case CommonEnum::COMMAND_DISPLAY_DONE: {
				output = executeDisplayDone();
				break;
			}
			case CommonEnum::COMMAND_EXIT: {
				executeExit();
				break;
			}
			case CommonEnum::COMMAND_INVALID: {
				throw BotException(BotException::EXCEPTION_COMMAND_INVALID);
				break;
			}
		}
		
	}
	catch (BotException& e) {
		output.statusMsg=e.what();
	}

	delete _tokeniser;
	_tokeniser = NULL;
	return output;
}

CommonEnum::ReturnStatus BotLogic::getDefaultView() {
	return _allEntries.displayCommand();
}

QStringList BotLogic:: getAllNames(){
	return _allEntries.getNameVect();
}

CommonEnum::ReturnCount BotLogic::getCount(){
	return _allEntries.getCount();
}

bool BotLogic::checkInputFormat(string input){

	if(Tokeniser::checkFormat(input)){
		return true;
	}else{
		return false;
	}
}

vector<Entry*> BotLogic::getVector() {
	return _allEntries.getActive();
}

CommonEnum::ReturnStatus BotLogic::executeAdd(){
	Entry* entry = _tokeniser->getEntry();
	return _allEntries.addCommand(entry);
}
CommonEnum::ReturnStatus BotLogic::executeDelete(){
	int lineToDel = _tokeniser->extractLineNum();
	return _allEntries.deleteCommand(lineToDel);
}
CommonEnum::ReturnStatus BotLogic::executeSetDone(){
	int lineToDel = _tokeniser->extractLineNum();
	return _allEntries.setDoneCommand(lineToDel);
}

CommonEnum::ReturnStatus BotLogic::executeEdit(){
	int lineToEdit = _tokeniser->extractLineNum();
	vector<string> newEntry = _tokeniser->extractEntryToEdit();
	return _allEntries.editCommand(lineToEdit, newEntry);
}

CommonEnum::ReturnStatus BotLogic::executeDisplay(){
	return _allEntries.displayCommand();
}
CommonEnum::ReturnStatus BotLogic::executeDisplayAll(){
	return _allEntries.displayAllCommand();
}
CommonEnum::ReturnStatus BotLogic::executeSearch(){
	string key = _tokeniser->extractKey(); 
	return _allEntries.searchCommand(key);
}
CommonEnum::ReturnStatus BotLogic::executeUndo(){
	return _allEntries.undoCommand();
}
CommonEnum::ReturnStatus BotLogic::executeRedo(){
	return _allEntries.redoCommand();
}
CommonEnum::ReturnStatus BotLogic::executeDisplayTask(){
	return _allEntries.searchTasks();
}
CommonEnum::ReturnStatus BotLogic::executeDisplayEvent(){
	return _allEntries.searchEvents();
}
CommonEnum::ReturnStatus BotLogic::executeDisplayWeek(){
	return _allEntries.searchWeek();
}
CommonEnum::ReturnStatus BotLogic::executeDisplayDay(){
	string key = _tokeniser->extractKey();
	QDate target = _tokeniser->formatDate(key);
	return _allEntries.searchDay(target);
}

CommonEnum::ReturnStatus BotLogic::executeDisplayDayBefore(){
	string key = _tokeniser->extractKey();
	QDate target = _tokeniser->formatDate(key);
	return _allEntries.searchDayBefore(target);
}
CommonEnum::ReturnStatus BotLogic::executeDisplayPastNotDone(){
	string key = _tokeniser->extractKey();
	QDate target = _tokeniser->formatDate(key);
	return _allEntries.searchDayPastNotDone(target);
}
CommonEnum::ReturnStatus BotLogic::executeClear(){
	return _allEntries.clearCommand();
}
CommonEnum::ReturnStatus BotLogic::executeDisplayDone(){
	return _allEntries.searchStatus(1);
}
CommonEnum::ReturnStatus BotLogic::executeDisplayNotDone(){
	return  _allEntries.searchStatus();
}
CommonEnum::ReturnStatus BotLogic::executeExit(){
	exit(0);
}